package cn.yq.aio.utils.recycler;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

/**
 * @AUTHOR YangLu
 * @Time 2021-03-25
 * @todo grid 分割线
 */
public class GridItemDecoration extends RecyclerView.ItemDecoration {

    int right, bottom;

    public GridItemDecoration(int right, int bottom) {
        this.right = right / 2;
        this.bottom = bottom / 2;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent,
                               @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int position = parent.getLayoutManager().getPosition(view);
        int childCount = parent.getAdapter().getItemCount();
        int count = 1;
        if (parent.getLayoutManager() instanceof GridLayoutManager) {
            count = ((GridLayoutManager) parent.getLayoutManager()).getSpanCount();
        }
        int l = 0, r = 0, t = 0, b = 0;
        if (position % count == 0) {
            r = right;
        } else if (position % count == count - 1) {
            l = right;
        } else {
            r = right;
            l = right;
        }
        if (position >= count) {
            t = bottom;
        }
        if (position < childCount - count) {
            b = bottom;
        }
        outRect.set(l, t, r, b);
    }
}
